import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-village-clear-total',
	templateUrl: './village-clear-total.component.html',
	styleUrls: ['./village-clear-total.component.css']
})
export class VillageClearTotalComponent implements OnInit {
	tableData = []; //table数据
	resultTableData:any = [];
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	startTime_table:string = '';
	endTime_table:string = '';
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	carNumber:string = '';
	deviceId:string = '';
	timeType:string='1'; //时间类型
	jumpData:any; // 跳转页面接收到的数据
    initDateRange: any = {
        startTime:'',
        endTime:'',
    }// 时间组件需要渲染的值
	columnSortFlag:number;//table列 排序
    carDeatailUseList = carTypeListObj.carDeatailUseList
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}

	ngOnInit() {
	    let date = new Date();
	    date.setDate(date.getDate() - 1);
        let yersterday = common.getTimeDay(date);
        this.initDateRange.startTime = this.startTime_table = yersterday+' 00:00:00';
        this.initDateRange.endTime = this.endTime_table = yersterday+' 23:59:59';
		this.load();
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.startTime_table = time.startTime;
		this.endTime_table = time.endTime;
	} 
	//公司选中回调
    companySelected(data){
        this.selectedCompanyId = data.companyId;
        this.selectedCompanyName = data.companyName;
        this.deviceId = data.deviceId;
        this.carNumber = data.carNumber;
    }
	//表格搜索
	table_search() {
	    this.columnSortFlag = null;
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh(){
    	this.load();
    }
	//初始化加载list数据
	load() {
	    if(!this.startTime_table||!this.endTime_table){
            return this.jsonApiService.message('起始或结束时间不能为空');
        }
        let timeCheck=common.correctTimeParams(this.startTime_table,this.endTime_table,60);
        if(!timeCheck.status){
            this.jsonApiService.message(timeCheck.message);
            return;
        }
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				// companyId: this.selectedCompanyId,
				// deviceId: this.deviceId,
				startTime:this.startTime_table,
				endTime: this.endTime_table,
				searchKey: this.searchKey,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("clearStatistics/CommunityClearStatisticsFind.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
            this.totalCount = this.tableData.length;
            this.client_pages(0,this.pageSize);
		})
	}
	client_pages(start,length){
        this.resultTableData = this.tableData.slice(start,start+length);
        for(let item of this.resultTableData) {
            item.weight = item.weight && parseInt(item.weight).toFixed(2);
        }
    }
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.client_pages(e.first,e.rows);
	}
	//导出报表
	exportData() {
		if(!this.startTime_table||!this.endTime_table){
			return this.jsonApiService.message('起始或结束时间不能为空');
		}
		let timeCheck=common.correctTimeParams(this.startTime_table,this.endTime_table,60);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let parameters = {
				// companyId: this.selectedCompanyId,
				// deviceId: this.deviceId,
				startTime:this.startTime_table,
                endTime: this.endTime_table,
				searchKey: this.searchKey,
			};
		let paramsName = 'param';
		let _url = config.BaseURL + 'environmentalCarQingyunDetails/exportEnvironmentalCarQingyunStatisList.html';
		common.exprotDataFile(parameters,paramsName,_url);
	}
	// 重量异常明细
    rowDetail(Url,row){
        row.startTime = this.startTime_table;
        row.endTime = this.endTime_table;
        let BaseURL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
        common.setCookie('jumpTo'+Url, JSON.stringify(row));
        window.open(BaseURL+'#'+Url);
    }
	DetailTableData: any = [];
	showDetail(row,modal){
		this.DetailTableData = JSON.parse(JSON.stringify(row.list));
		modal.show();
	}
	cancelDetailModal(modal) {
		this.DetailTableData = [];
		modal.hide();
	}
}